import smtplib
from email.mime.text import MIMEText
# email 用于构建邮件内容
from email.header import Header
import random


def sendEmail(to_addr, code):
    from_addr = 'yuransaximoer@163.com'
    password = 'GCDQQXVCIZACKOYD'

    smtp_server = 'smtp.163.com'

    # 邮箱正文内容，第一个参数为内容，第二个参数为格式(plain 为纯文本)，第三个参数为编码
    msg = MIMEText(str(code), 'plain', 'utf-8')

    # 开启发信服务，这里使用的是加密传输
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header('验证码')
    server = smtplib.SMTP_SSL(smtp_server)
    server.connect(smtp_server, 465)
    # 登录发信邮箱
    server.login(from_addr, password)
    # 发送邮件
    server.sendmail(from_addr, to_addr, msg.as_string())
    # 关闭服务器
    server.quit()


if __name__ == '__main__':
    code = random.randint(1000, 10000)
    sendEmail('1184323543@qq.com', code)
